package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.FindNewsResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;

import java.util.List;

import static com.fanhao.dllo.soundfm.entity.FindNewsResult.*;

/**
 * Created by dllo on 16/1/21.
 */
public class FindNewsAdapter extends RecyclerView.Adapter
        <FindNewsAdapter.FindViewHolder> {
    private Context context;
    private List<FindNewsResult.ResultEntity.ActivityDataEntity> activityList;
    private MyOnclickListener myOnclickListener;

    public void setMyOnclickListener(MyOnclickListener myOnclickListener) {
        this.myOnclickListener = myOnclickListener;
    }

    public FindNewsAdapter(Context context) {
        this.context = context;
    }

    public void addActivityData(List<FindNewsResult.ResultEntity.ActivityDataEntity> activityList) {
        this.activityList = activityList;
        notifyDataSetChanged();
    }

    @Override
    public FindViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_find_news_adapter_item, parent, false);
        return new FindViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final FindViewHolder holder, final int position) {
        if (activityList != null && activityList.size() > 0) {
            holder.titleFindNewsTv.setText(activityList.get(position).getTitle());
            holder.findNewsIv.setImageUrl(
                    activityList.get(position).getCoverPathLarge(), BaseFragment.getImageLoader());
            holder.itemPos = position;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myOnclickListener.myNoListener("",activityList.get(position).getActivityId());
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        return activityList != null && activityList.size() > 0 ?
                activityList.size() : 0;
    }

    public class FindViewHolder extends RecyclerView.ViewHolder {
        CardView cardViewFindNews;
        TextView titleFindNewsTv;
        NetworkImageView findNewsIv;
        private int itemPos;

        public FindViewHolder(View itemView) {
            super(itemView);
            cardViewFindNews = (CardView) itemView.findViewById(R.id.cardView_findNewsAdapter);
            titleFindNewsTv = (TextView) itemView.findViewById(R.id.tvTitle_findNewsAdapter);
            findNewsIv = (NetworkImageView) itemView.findViewById(R.id.iv_findNewsAdapter);
        }
    }
}
